package com.molichuxing.services.infrastructure.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.BlacklistBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.BlacklistMapper;
import com.molichuxing.services.infrastructure.dto.request.create.BlacklistCreateDto;
import com.molichuxing.services.infrastructure.dto.response.BlacklistDto;
import com.molichuxing.services.infrastructure.service.BlacklistService;

/**
 * 
 * @author liyg
 * 黑名单服务实现类
 * 2019-08-15
 * 
 */
@Service("blacklistService")
public class BlacklistServiceImpl implements BlacklistService{
	
	@Autowired
	private BlacklistMapper blacklistMapper;

	/**
	 * 获取黑名单列表
	 * 
	 * @param map
	 *            参数
	 *key:name
	 *            String 用户名
	 *key:phone
	 *            String 用户手机号
	 *key:idNumber
	 *            String 身份证号
	 *key:startTime
	 *            LocalDateTime 拉黑开始时间
	 *key:endTime
	 *            LocalDateTime 拉黑结束时间
	 *key:type
	 *            Integer 拉黑方式
	 *key:pageNum
	 *            Integer 当前页
	 *key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<BlacklistDto> getBlacklistList(Map<String, Object> map) {
		
		// 查询总条数
		long total = blacklistMapper.getBlacklistPageCount(map);
		if(total <= 0) {
			return null;
		}

		/*分页查询列表*/
		Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
		List<BlacklistBean> list = blacklistMapper.getBlacklistPage(map,pageNum,pageSize);
		List<BlacklistDto> blacklistDtoList = Convert.toBlacklistDto(list);
		Paged<BlacklistDto> paged = new Paged<>(blacklistDtoList, total ,pageSize);
				
		return paged;
	}
	
	/**
	 * 获取黑名单详情
	 * @param phone
	 * @return
	 */
	@Override
	public BlacklistDto getBlacklist(String phone) {
		
		BlacklistBean bean = blacklistMapper.getBlacklistByPhone(phone);
		BlacklistDto blacklistDto = Convert.toBlacklistDto(bean);
		
		return blacklistDto;
	}
	
	/**
	 * 获取黑名单详情
	 * @param id
	 * @return
	 */
	@Override
	public BlacklistDto getBlacklist(Integer id) {
		
		BlacklistBean bean = blacklistMapper.getById(id);
		BlacklistDto blacklistDto = Convert.toBlacklistDto(bean);
		
		return blacklistDto;
	}

	/**
	 * 创建黑名单
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean createBlacklist(BlacklistCreateDto createDto) {
		
		BlacklistBean blacklist = blacklistMapper.getBlacklistByPhone(createDto.getPhone());
		if(null != blacklist) {
			throw  new BizException(105, "该用户已经被拉入黑名单，请不要重复操作！");
		}
		
		BlacklistBean bean = new BlacklistBean();
		BeanUtils.copyProperties(createDto, bean);
		bean.setAccountId(createDto.getOperateId());
		bean.setAccountAccount(createDto.getOperateName());
		
		int num = blacklistMapper.create(bean);
		
		return num== 1 ? true : false;
	}
	
	/**
	 * 逻辑删除黑名单
	 * 
	 * @param blacklistId
	 *            黑名单id
	 * @param accountId
	 *            操作人id
	 * @param accountAccount
	 *            操作人名称
	 * @return
	 */
	@Override
	public Boolean deleteBlacklist(Integer blacklistId,Integer accountId,String accountAccount) {
		
		BlacklistBean bean = new BlacklistBean();
		bean.setId(blacklistId);
		bean.setAccountId(accountId);
		bean.setAccountAccount(accountAccount);
		bean.setIsDelete(true);
		bean.setOperationTime(DateUtil.convertDateToLDT(new Date()));
		int num = blacklistMapper.delete(bean);
		
		return num == 1 ? true : false;
	}
	
	/**
	 * 移除黑名单
	 * @param idNumber
	 * @return
	 */
	@Override
	public Boolean modifyBlacklist(String idNumber) {
		int num = blacklistMapper.modifyByIdNumber(idNumber,LocalDateTime.now());
		return num > 0;
	}
}
